<?php
	// Module name to be created 
	$modulename = $argv[1];
	// including the tablespace
	$tablename = $argv[2];
	// make the directory
	mkdir('modules/'.$modulename,0777);
	// open the file to for the model
	$fh = fopen('modules/'.$modulename.'/model.class.php', 'w+');
	// build the model file string
	$model_string = "<?php

require_once(\"libs/adodb/adodb.inc.php\");
require_once(\"config/dbconfig.php\");

class ".$modulename." {
	
	const SQL_INSERT = \"INSERT INTO ".$tablename."(<fields>) VALUES (<?>)\";
	const SQL_SELECT = \"SELECT <fields> FROM ".$tablename."\";
	const SQL_UPDATE = \"UPDATE ".$tablename." SET <fields>\";

	private \$dbc;
	
	function __construct(){
		try {
			\$this->dbc = ADONewConnection(getDBString());
		} catch(exception \$e){
			adodb_backtrace(\$e->gettrace());
			return false;
		}
	}
	
	function __destruct(){
		\$this->dbc->Close();
	}
	
	public function add(\$cond){
		try {
			\$fields = '';
			\$qsign = '';
			\$values = array();
			foreach (\$cond as \$column => \$value){
				\$fields .= \$column.',';
				\$qsign .= '?,';
				\$values[] = \$value;
			}

			\$fields = substr(\$fields,0,strlen(\$fields)-1);
			\$qsign = substr(\$qsign,0,strlen(\$qsign)-1);
				
			\$str_sql = str_ireplace('<fields>',\$fields,self::SQL_INSERT);
			\$str_sql = str_ireplace('<?>',\$qsign,\$str_sql);

			\$statement = \$this->dbc->Prepare(\$str_sql);
			\$this->dbc->Execute(\$statement,\$values);

		} catch(exception \$e) {
			adodb_backtrace(\$e->gettrace());
			return false;
		}
		return true;
	}

	public function update(\$udata,\$condition){
		try {
			\$fields = '';
			\$values = array();

			foreach(\$udata as \$column => \$value){
				\$fields .= \$column.' = ?,';
				\$values[] = \$value;
			}

			\$fields = substr(\$fields,0,strlen(\$fields)-1);

			\$str_sql = str_ireplace('<fields>',\$fields,self::SQL_UPDATE);

			\$cond_str = '';
			foreach(\$condition as \$column => \$value){
				\$cond_str .= \$column.' = ? AND ';
				\$values[] = \$value;
			}

			\$cond_str = substr(\$cond_str,0,strlen(\$cond_str)-5);
			
			\$str_sql .= ' WHERE '.\$cond_str;
			\$statement = \$this->dbc->Prepare(\$str_sql);

			\$this->dbc->Execute(\$statement,\$values);

		} catch(exception \$e) {
			adodb_backtrace(\$e->gettrace());
			return false;
		}
		return true;
	}

	public function get(\$fields='*',\$condition=array()){
		try {
			\$str_sql = str_ireplace('<fields>',\$fields,self::SQL_SELECT);
			
			if(count(\$condition) > 0){
				\$cond_fields = '';
				\$values = array();

				foreach (\$condition as \$column => \$value){
					\$cond_fields .= \$column.' = ? AND ';
					\$values[] = \$value;
				}

				\$cond_fields = substr(\$cond_fields,0,strlen(\$cond_fields)-5);
				\$str_sql .= ' WHERE '.\$cond_fields;
				\$statement = \$this->dbc->Prepare(\$str_sql);
				\$result = \$this->dbc->Execute(\$statement,\$values);
			} else {
				\$statement = \$this->dbc->Prepare(\$str_sql);
				\$result = \$this->dbc->Execute(\$statement);
			}	

			if(!\$result){
				adodb_backtrace();
				return false;
			} else {
				if(\$result->RecordCount() == 1){
					if(count(\$result->fields) == 2){
						\$rvalue = trim(\$result->fields[0]);
						\$result->Close();
						return \$rvalue;	
					} else {
						\$reg = array();
						foreach(\$result->fields as \$column => \$value){
							if(gettype(\$column) == 'string') \$reg[\$column] = \$value;
						}
						\$result->Close();
						return \$reg;
					}
				} else {
					\$rarray = array();
					while(!\$result->EOF){
						\$reg = array();
						foreach(\$result->fields as \$column => \$value){
							if(gettype(\$column) == 'string') \$reg[\$column] = \$value;
						}
						\$rarray[] = \$reg;
						\$result->MoveNext();
					}
					\$result->Close();
					return \$rarray;
				}
			}
		} catch(exception \$e){
			adodb_backtrace(\$e->gettrace());
			return false;
		}
	}

	public function delete(\$condition){
		try {
			\$str_query = str_ireplace('<fields>','deleted = ?',self::SQL_UPDATE);
			\$str_cond = '';
			\$values = array('t');
			foreach(\$condition as \$column => \$value){
				\$str_cond .= \$column.' = ? AND ';
				\$values[] = \$value;
			}
			\$str_cond = substr(\$str_cond,0,strlen(\$str_cond)-5);
			\$str_query .= ' WHERE '.\$str_cond;
			\$statement = \$this->dbc->Prepare(\$str_query);
			\$this->dbc->Execute(\$statement,\$values);
		} catch(exception \$e) {
			adodb_backtrace(\$e->gettrace());
			return false;
		}
		return true;
	}

	public function selectableRecordSet(\$value,\$to_s){
		\$ret = '';
		\$all = \$this->get('*',array('deleted' => 'f'));
		foreach(\$all as \$record){
			\$ret .= '<option value=\"'.htmlentities(\$record[\$value],ENT_NOQUOTES,'UTF-8').'\">'.htmlentities(\$record[\$to_s],ENT_NOQUOTES,'UTF-8').'</option>';
		}
		return \$ret;
	}

	public function selectableSetByParent(\$parent,\$value,\$to_s){
		\$ret = '';
		\$all = \$this->get('*',array('deleted' => 'f', 'parent_id' => \$parent));
		foreach(\$all as \$record){
			\$ret .= '<option value=\"'.htmlentities(\$record[\$value],ENT_NOQUOTES,'UTF-8').'\">'.htmlentities(\$record[\$to_s],ENT_NOQUOTES,'UTF-8').'</option>';
		}
		return \$ret;
	}
}

?>";
	// write the model to the file
	fwrite($fh, $model_string);

	// closing the file
	fclose($fh);
?>
